const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemList:[],
    money:0,
    coupon:[],
    showActionsheet:false,
    couponList:[],
    couponIdx:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let t = this;
    const eventChannel = t.getOpenerEventChannel();
    eventChannel.on('itemList', function(itemList) {
      console.log(itemList)
      let arr = [],money=0;
      itemList.forEach(m=>{
        m.itemDetailList.forEach(i=>{
          i.active && (
            money += i.count.count*i.one_price,
            i.itemCount = i.count.count,
            i.title = m.title, arr.push(i)
          )
        })
      })
      t.setData({itemList:arr, money:money})
    })
    t.getCouponList(t)
  },
  getCouponList(t){
    app.request({
      url:'https://live.sgwxmini.com/beauty/rowNumber/getCouponList',
      data:{
        hosId:app.api.siteinfo.hosId,
        customerId:wx.getStorageSync('customerId'),
      },
      success(e){
        e.data.couponList.length && t.setData({
          couponList:e.data.couponList.map(i=>{
            i.text = i.coupon_name;
            i.value = i.id;
            return i;
          })
        })
      }
    })
  },

  payment(){
    let d = this.data;
    wx.showLoading({mask:true})
    app.request({
      url:'https://live.sgwxmini.com/beauty/rowNumber/insertTrans',
      data:{
        hosId:app.api.siteinfo.hosId,
        customerId:wx.getStorageSync('customerId'),
        // customerId:'14701412623950100',
        yyItemArray:JSON.stringify(d.itemList),
        couponId:d.couponList.length?d.couponList[d.couponIdx].id:'',
        payHosId:app.api.siteinfo.payId
      },
      success(e){
        console.log(e)
        wx.requestPayment({
          ...e.data,
          success(e) {
            wx.showToast({title:'支付成功'})
            wx.navigateBack({delta: 1})
          },
          fail(err){
            wx.showToast({icon:'none',title:'支付失败'})
          },
          complete:wx.hideLoading
        })
      }
    })
  },
  close() {
    if(!this.data.couponList.length) return;
    this.setData({
      showActionsheet: !this.data.showActionsheet
    })
  },
  btnClick(e) {
    this.setData({
      couponIdx:e.detail.index
    })
    this.close()
  }
})